// import { configurationenvironment } from './environment.js';

export function getUrlParam(url){ //获取二维码解析后的参数
	let arr = url.split("?")[1].replace(/=/g,":").split('&');
	let obj = {};
	for (var i = 0; i < arr.length; i++) {
	　  var split = arr[i].split(':')
	　　obj[split[0]] = split[1]
	}
	return obj;
}

export function operation(num1, num2, op){ //计算点与点之间的距离
	let l1, l2, max;
	try {
	    l1 = num1.toString().split(".")[1].length;
	} catch (e) {
	    l1 = 0;
	}
	try {
	    l2 = num2.toString().split(".")[1].length;
	} catch (e) {
	    l2 = 0;
	}
	switch (op) {
	    case "plus":
		    max = Math.pow(10, Math.max(l1, l2));
		    return (num1 * max + num2 * max) / max;
	    case "subtract":
		    max = Math.pow(10, Math.max(l1, l2));
		    return (num1 * max - num2 * max) / max;
	    case "multiply":
		    return (Number(num1.toString().replace(".", "")) * Number(num2.toString().replace(".", ""))) / Math.pow(10, l1 + l2);
	    case "divide":
		    return operation(Number(num1.toString().replace(".", "")) / Number(num2.toString().replace(".", "")), Math.pow(10, l2 - l1), "multiply");
	}
}

export function getLocation(comfirmCb) { //获取用户当前位置
	return new Promise((resolve, reject) => {
		const { longitude, latitude } = uni.getStorageSync('localtion');
		uni.getLocation({
			//#ifdef MP-WEIXIN
			type: "gcj02",
			//#endif
			success: res => {
				uni.setStorageSync("localtion", res);
			    resolve(res);
			},
			fail: res => {
				//#ifdef MP-WEIXIN
				if (res.errMsg.startsWith('getLocation:fail 频繁调用')) {
				    resolve({ longitude, latitude });
				} else if (res.errMsg !== 'getLocation:fail auth deny') {
					return uni.showModal({
						title: `"${this.globalData.brandName}"需要获取您的地理位置`,
						content: "请检查您的手机系统定位功能是否正常，微信定位是否已授权",
						confirmText: '知道了',
						showCancel: false,
						complete: (res) => {
							reject();
						}
					});
				}
				uni.getSetting({
					success: res => {
						var statu = res.authSetting;
						if (statu['scope.userLocation'] === false) {
							uni.showModal({
								title: '是否授权当前位置',
								content: '需要获取您的地理位置，请确认授权，否则地图功能将无法使用',
								showCancel: false,
								success: (res) => {
								    if (res.confirm) {
									    uni.openSetting({
										    success: data => {
												if (data.authSetting['scope.userLocation']) {
													uni.showToast({
														title: '授权成功',
														icon: 'success'
													});
													getLocation();
													comfirmCb && comfirmCb();
												} else {
													uni.showToast({
														title: '授权失败',
														icon: 'none'
													});
												}
									    	},
									    });
									}else if (res.cancel ) {
										reject();
									}
								},
							});
						}
					},
					fail: () => {
						uni.showToast({
							title: '调用授权窗口失败',
							icon: 'none'
						});
					},
				});
				//#endif
				//#ifdef MP-ALIPAY
		 			my.showAuthGuide({ 
		 				authType:'LBS',
		 				success:(res)=>{},
		 				fail:(err)=>{},
		 			});	
				//#endif
			}
		})
	})
}

export function unique(arr, attr) { //去重
	if (arr.length > 0) {
		const res = new Map();
		return arr.filter((item) => !res.has(item[attr]) && res.set(item[attr], 1));
	} else {
		return [];
	}
}

export function uploadAvatarRes(filePath, fileType){
	const uploadImgUrl = `http://47.94.44.214:8080/file/upload`; // 上传的图片服务器地址
	return new Promise((resolve, reject) => {
		uni.uploadFile({
			url: uploadImgUrl,
			fileType,
			fileName: "file",
			filePath,
			name: "file",
			formData: {
				fileType,
				fileName: "file",
				filePath,
			},
			success: (res) => {
				let data = JSON.parse(res.data);
				resolve(data);
			},
			fail: (err) => {
				reject(err);
			},
		});
	});
}

// export function uploadFileRes(filePath, fileType){ //上传图片
// 	let ServiceEnvironment = uni.getStorageSync("ServiceEnvironment"); //当前环境状态
// 	let urlConfig =  configurationenvironment(ServiceEnvironment);
// 	return new Promise((resolve, reject) => {
// 		uni.uploadFile({
// 			url: urlConfig + "/admin/image/toUploadImage", //仅为示例，非真实的接口地址
// 			fileType,
// 			fileName: "file",
// 			filePath,
// 			name: "file",
// 			formData: {
// 				fileType,
// 				fileName: "file",
// 				filePath,
// 			},
// 			success: (uploadFileRes) => {
// 				let data = JSON.parse(uploadFileRes.data);
// 				resolve(data.imageUrl);
// 			},
// 			fail: (err) => {
// 				reject(err);
// 			},
// 		});
// 	});
// }

// export function cameraUploadFileRes(filePath, plateNo){
// 	let ServiceEnvironment = uni.getStorageSync("ServiceEnvironment"); //当前环境状态
// 	let urlConfig =  configurationenvironment(ServiceEnvironment);
// 	return new Promise((resolve, reject) => {
// 		uni.uploadFile({
// 			url: urlConfig + "/bike/aiSign/check", //仅为示例，非真实的接口地址
// 			fileName: "file",
// 			filePath,
// 			name: "file",
// 			plateNo,
// 			formData: {
// 				fileName: "file",
// 				filePath,
// 			},
// 			success: (uploadFileRes) => {
// 				let data = JSON.parse(uploadFileRes.data);
// 				resolve(data.imageUrl);
// 			},
// 			fail: (err) => {
// 				reject(err);
// 			},
// 		});
// 	});
// }
